﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Jocker;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace ManagoMes.Read.API.Applications.ProductionScheduling
{
    public class TransferHtmlHandler : IRequestHandler<TransferHtmlCommond, ResultsDTO<DiversionDto>>
    {

        private readonly IBateRepository<DiversionDto> _TransferTable;

        public TransferHtmlHandler(IBateRepository<DiversionDto> transferTable)
        {
            _TransferTable = transferTable;
        }

        public async Task<ResultsDTO<DiversionDto>> Handle(TransferHtmlCommond request, CancellationToken cancellationToken)
        {
            try
            {
                var list = new ResultsDTO<DiversionDto>() { Code = 0 };
                var str = "SELECT * from  transfertable join warehouselist on transfertable.StashId =warehouselist.StashId";
                var ctrdate = new DiversionDto();
                DapperHelper dapperHelper = new DapperHelper();
                var hte = _TransferTable.GetAll(str, ctrdate, dapperHelper.MangoMesRead).ToList();

                var str1 = "SELECT a.TransferOrderNumber,a.StashId,a.StashIds,a.TransferOrderName,b.StashName,c.StashName as this from  transfertable a join warehouselist b on a.StashIds=b.StashId JOIN warehouselist c ON a.StashId=c.StashId";


                var ctrdates = new DiversionDto();
                var htes = _TransferTable.GetAll(str1, ctrdate, dapperHelper.MangoMesRead).ToList();


                if (!string.IsNullOrEmpty(request.TransferOrderName))
                {
                    hte = hte.Where(x => x.TransferOrderName == request.TransferOrderName).ToList();
                    htes = htes.Where(x => x.TransferOrderName == request.TransferOrderName).ToList();
                }
                if (!string.IsNullOrEmpty(request.TransferOrderNumber))
                {
                    hte = hte.Where(x => x.TransferOrderNumber == request.TransferOrderNumber).ToList();
                    htes = htes.Where(x => x.TransferOrderNumber == request.TransferOrderNumber).ToList();
                }
                if (request.isType > -1)
                {
                    hte = hte.Where(x => x.isType == request.isType).ToList();
                    htes = htes.Where(x => x.isType == request.isType).ToList();
                }
                if (request.StashId > 0)
                {
                    hte = hte.Where(x => x.StashIds == request.StashId).ToList();
                    htes = htes.Where(x => x.StashIds == request.StashId).ToList();
                }
                if (request.StashIds > 0)
                {
                    hte = hte.Where(x => x.StashId == request.StashIds).ToList();
                    htes = htes.Where(x => x.StashId == request.StashIds).ToList();
                }

                List<DiversionDto> newDivdate = new List<DiversionDto>();
                for (int i = 0; i < hte.Count; i++)
                {
                    DiversionDto diversionDto = new DiversionDto();
                    diversionDto.infoMess = hte[i].infoMess;
                    diversionDto.TransferOrderNumber = hte[i].TransferOrderNumber;
                    diversionDto.TransferOrderName = hte[i].TransferOrderName;
                    diversionDto.Transfertime = hte[i].Transfertime;
                    diversionDto.Transferid = hte[i].Transferid;
                    diversionDto.isType = hte[i].isType;
                    diversionDto.StashId = hte[i].StashId;
                    diversionDto.StashIds = hte[i].StashIds;
                    diversionDto.StashName = hte[i].StashName;
                    diversionDto.StasTypename = hte[i].isType == 0 ? "内部转移" : "外部转移";
                    diversionDto.StashdName = htes[i].StashName;

                    newDivdate.Add(diversionDto);
                }
                list.Data = newDivdate;
                return list;
            }
            catch (Exception)
            {

                throw;
            }




        }
    }
}
